package com.dnxh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author 枳湫
 * @Time 2022/4/6 15:12
 * @Describe Basic_jar
 **/
@Configuration
public class RedisConfig {
	
	/**
	 * 配置Redis 设置Redis编码格式
	 * @param redisConnectionFactory redisConnectionFactory
	 * @return redisTemplate
	 */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
		RedisTemplate<String, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer =
				new Jackson2JsonRedisSerializer<>(Object.class);
		template.setKeySerializer(stringRedisSerializer);
		template.setValueSerializer(objectJackson2JsonRedisSerializer);
		template.setHashKeySerializer(stringRedisSerializer);
		template.setHashValueSerializer(objectJackson2JsonRedisSerializer);
		return template;
	}
}
